package com.ywc.tools.contentprovide;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ywc.tool.PermissionUtil;
import com.ywc.tools.R;
import com.ywc.tools.adapter.BaseViewHold;
import com.ywc.tools.adapter.ListAdapter;

import java.util.ArrayList;
import java.util.List;

public class ProvideFragment1 extends Fragment{

    private View view;
    private ListView listView;
    private ProvideAdapter provideAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view==null)
            view=inflater.inflate(R.layout.fragment_provide1,container,false);
        init();
        if (PermissionUtil.isOpen(getActivity(),1, Manifest.permission.READ_CONTACTS))
            initData();
        return view;
    }


    private void init() {
        listView = ((ListView) view.findViewById(R.id.list));
        provideAdapter = new ProvideAdapter(getActivity());
        listView.setAdapter(provideAdapter);
    }

    private void initData() {
        /**
         *
         * @param uri 查询某个应用程序下的某一张表
         * @param projection 指定查询的列名
         * @param selection  指定where的约束条件
         * @param selectionArgs 为where中的占位符提供具体值
         * @param sortOrder 指定查询结果的排序方式
         * @return
         */

        Cursor cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null,
                null, null);
        if (cursor!=null)
        {
            List<Contacts> list=new ArrayList<>();
            while (cursor.moveToNext())
            {
                Contacts contacts = new Contacts();
                contacts.setName(  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                contacts.setPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                list.add(contacts);
            }
            provideAdapter.flush(list);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==1&&PermissionUtil.processing(permissions, grantResults)==null)
        {
            initData();
        }
    }




    class ProvideAdapter extends ListAdapter<Contacts>
    {

        private TextView name;
        private TextView num;

        public ProvideAdapter(Context context) {
            super(context, R.layout.adapter_fragment_contacts);
        }

        @Override
        public void setData(int position, BaseViewHold baseViewHold, Contacts contacts) {
            name = ((TextView) baseViewHold.fdView(R.id.name));
            num = ((TextView) baseViewHold.fdView(R.id.num));
            name.setText(contacts.getName());
            num.setText(contacts.getPhone());
        }
    }
}
